import { CategoryState } from './../../api/product/attr/type';

import { defineStore } from 'pinia';
import { getCategory1, getCategory2, getCategory3 } from '@/api/product/attr';
import { AttrList } from '@/api/product/attr/type';
import type { Response } from '@/api/type';

// 小仓库：分类相关仓库
// 仓库的名称：CategoryStore
export const useCategoryStore = defineStore('CategoryStore', {
  state: (): CategoryState => {
    return {
      category1Id: null,
      category2Id: null,
      category3Id: null,
      category1List: [] as AttrList,
      category2List: [] as AttrList,
      category3List: [] as AttrList,
    }
  },
  actions: {
    async getCategoryList() {
      let result: Response<AttrList> = await getCategory1();
      if (result.code === 200) {
        this.category1List = result.data || [];
        return 'ok';
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    async getCategory2List(category1Id: number) {
      let result: Response<AttrList> = await getCategory2(category1Id);
      if (result.code === 200) {
        this.category2List = result.data || [];
        return 'ok';
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    async getCategory3List(category2Id: number) {
      let result: Response<AttrList> = await getCategory3(category2Id);
      if (result.code === 200) {
        this.category3List = result.data || [];
        return 'ok';
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
  },
  getters: {

  }
})